package com.edu.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.CommentDTO;
import com.edu.entity.CommonResult;
import com.edu.entity.ContentVote;
import com.edu.entity.Page;
import com.edu.entity.UserComents;
import com.edu.service.UserComentsService;
import com.edu.service.impl.UserComentsServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/AddUserComents")
public class addUserComentServlet extends HttpServlet {
	UserComentsService ucs = new UserComentsServiceImpl();
	CommonResult cr = new CommonResult();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ObjectMapper om = new ObjectMapper();
		List<CommentDTO> listUsc = new ArrayList<CommentDTO>();
		int rowNum;
		String target_user_id_String = request.getParameter("targetid");// 商家id
		String user_id_string = request.getParameter("userId");// 用户id
		String comment_id_string = request.getParameter("commentId");// 评论id
//		System.out.println(target_user_id_String+"----"+user_id_string+"----"+comment_id_string);
		String voteType_string = request.getParameter("voteType");
		try {
			int target_user_id = Integer.parseInt(target_user_id_String);
//			System.out.println(target_user_id+"----");
			int user_id = Integer.parseInt(user_id_string);
//			System.out.println(target_user_id+"----"+user_id+"----");
			int comment_id = Integer.parseInt(comment_id_string);
			int voteType = Integer.parseInt(voteType_string);
//			System.out.println(target_user_id+"----"+user_id+"----"+comment_id);
//			c = ucs.getCommentsById(user_id, target_user_id,comment_id );
			ContentVote cv = new ContentVote(comment_id, user_id, voteType);
			ContentVote cVote = ucs.seleteContentVote(cv);
			cr.setData(cVote);
			if (cVote == null) {
				int result = ucs.addCotentVote(cv);
				if (result > 0) {
					cr.setCode(200);
					cr.setMessage("点赞成功");
				} else {
					cr.setCode(500);
					cr.setMessage("点赞失败");
				}
			} else {
				cv.setContentVoteId(cVote.getContentVoteId()); // 设置ID用于更新
//	           	System.out.println(cv.getVoteType());
				ucs.updateContentVote(cv);
				cr.setCode(200);
				cr.setMessage("投票已更新");
			}
			CommentDTO c = ucs.getCommentsById(user_id, target_user_id, comment_id);
			cr.setCode(200);
		} catch (Exception e) {
			int target_user_id = 0;
			int userId = 0;
			int voteType = 0;
			cr.setCode(404);
			cr.setMessage("fail");
		}
		response.setContentType("application/json;charset=utf-8");
//		System.out.println(cr.toJsonString());
		response.getWriter().write(cr.toJsonString());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ObjectMapper om = new ObjectMapper();
		List<CommentDTO> listUsc = new ArrayList<CommentDTO>();
		String sellerIdString = request.getParameter("sellerid");
		String userIdString = request.getParameter("userid");
		String myComent = request.getParameter("myComent");
		String myScoreString = request.getParameter("myScore");
		try {
			int sellerId = Integer.parseInt(sellerIdString);
			int userId = Integer.parseInt(userIdString);
			double myScore = Double.parseDouble(myScoreString);
			UserComents uc = new UserComents();
			uc.setUser_id(userId);
			uc.setTarget_user_id(sellerId);
			uc.setScore(myScore);
			uc.setContent(myComent);
			int nums = ucs.addComent(uc);
			if(nums==1) {
				cr.setCode(200);
				cr.setData("添加成功");
			}else {
				cr.setCode(404);
				cr.setData("添加失败");
			}
		} catch (Exception e) {
			cr.setCode(404);
			cr.setData("添加失败");
			System.out.println("出错了"+e);
		}
		response.setContentType("application/json;charset=utf-8");
//		System.out.println(cr.toJsonString());
		response.getWriter().write(cr.toJsonString());
	}
}
